var t, e = require("../../../@babel/runtime/helpers/defineProperty"), a = getApp();

Page({
    data: (t = {
        language: "",
        myvideo: "",
        goods_id: "",
        title: "",
        videoUrl: "",
        isVideo: !0,
        realVideo: "",
        coverUrl: "",
        duration: 0,
        chooseGoods: null,
        videoPath: "",
        videoCover: "",
        limit: 60,
        uploadIng: !1,
        videoProgress: 0,
        showUploadBtn: !1,
        imguploadIng: !1,
        isNeedGoods: !0,
        background: "",
        checked: !0,
        protocol_content: "",
        showAgreement: !1,
        theme_color: "#000",
        showGoods: !1
    }, e(t, "goods_id", ""), e(t, "categoryList", []), e(t, "categoryFilterList", []), 
    e(t, "category_id", null), e(t, "category_name", ""), e(t, "showCategory", !1), 
    e(t, "searchWord", ""), e(t, "is_show_cate", !1), t),
    onLoad: function(t) {
        this.data.myvideo = wx.createVideoContext("myvideo"), this.init(), this.getList(), 
        this.getPermission(), this.getBackground(), this.getAgreement();
        try {
            var e = wx.getStorageSync("shareVideoPath");
            if (e) {
                var a = JSON.parse(e);
                a.title && this.setData({
                    title: a.title
                }), a.videoUrl && this.setData({
                    videoUrl: a.videoUrl
                }), a.videoUrl && this.setData({
                    videoUrl: a.videoUrl,
                    videoPath: a.videoUrl
                }), a.videoCover && this.setData({
                    videoCover: a.videoCover
                }), a.category_id && this.setData({
                    category_id: a.category_id
                }), a.category_name && this.setData({
                    category_name: a.category_name
                });
            }
        } catch (t) {}
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    onShow: function() {
        var t = wx.getStorageSync("yz_basic_info");
        this.setData({
            theme_color: t.theme_color
        }), console.log(this.data.theme_color);
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    closeChooseGoods: function(t) {
        this.setData({
            showGoods: !1
        }), t.detail.goods_id != this.data.goods_id && t.detail.goods_id && (this.setData({
            goods_id: t.detail.goods_id
        }), this.getGoodsById());
    },
    init: function() {
        this.setData({
            title: "",
            videoUrl: "",
            isVideo: !0,
            duration: 0,
            chooseGoods: null,
            videoCover: "",
            limit: "",
            uploadIng: !1,
            showUploadBtn: !1,
            coverUrl: "",
            realVideo: "",
            videoPath: "",
            isNeedGoods: !0
        });
    },
    getBackground: function() {
        var t = this, e = a.getNetAddresss("plugin.video-share.frontend.set.getBackground");
        a._getNetWork({
            url: e,
            data: {
                validate_page: 1
            },
            success: function(e) {
                var a = e.data;
                1 == a.result ? t.setData({
                    background: a.data.background
                }) : wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getList: function() {
        var t = this, e = a.getNetAddresss("plugin.video-share.frontend.set.getLimit");
        a._getNetWork({
            url: e,
            data: {
                validate_page: 1
            },
            success: function(e) {
                var o = e.data;
                1 == o.result ? (t.setData({
                    is_show_cate: "1" == o.data.is_show_cate,
                    isNeedGoods: 1 == o.data.select_goods
                }), a._isTextEmpty(o.data.limit) || t.setData({
                    limit: o.data.limit
                })) : wx.showToast({
                    title: o.msg,
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getPermission: function() {
        var t = a.getNetAddresss("plugin.video-share.frontend.permission.verify");
        a._getNetWork({
            url: t,
            data: {},
            success: function(t) {
                var e = t.data;
                1 == e.result ? e.data.ret || wx.navigateBack({
                    delta: 1,
                    success: function() {
                        setTimeout(function() {
                            wx.showToast({
                                title: e.msg,
                                icon: "none",
                                duration: 1500
                            });
                        }, 1e3);
                    }
                }) : wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getGoodsById: function() {
        var t = this, e = a.getNetAddresss(1 == a.globalData.store_alone_temp ? "plugin.store-alone-temp.frontend.videoShare.goods.getGoodsById" : "plugin.video-share.frontend.goods.getGoodsById");
        a._getNetWork({
            url: e,
            data: {
                goods_id: this.data.goods_id
            },
            success: function(e) {
                var a = e.data;
                1 == a.result ? t.setData({
                    chooseGoods: a.data.goods
                }) : wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    onReadVideo: function() {
        var t = this;
        wx.chooseMedia({
            count: 1,
            mediaType: [ "video" ],
            sourceType: [ "album", "camera" ],
            maxDuration: 60,
            success: function(e) {
                t.getDuration(e.tempFiles[0]);
            }
        });
    },
    getDuration: function(t) {
        var e = wx.getStorageSync("yz_basic_info").upload_max_file_size, o = 100;
        return e && (o = e / 1024 / 1024), t.size / 1024 / 1024 > o ? (a.tips("视频大小超出".concat(o, "M，请压缩后上传！")), 
        void this.resetVideo()) : (this.data.duration = t.duration, this.data.duration < 1 ? (this.resetVideo(), 
        void a.tips("视频时长太短了！！")) : this.data.limit && this.data.duration > Number(this.data.limit) + 1 ? (this.resetVideo(), 
        void a.tips("视频时长不能超过".concat(this.data.limit, "秒"))) : (this.setSrcAndCaptureImage(t.tempFilePath), 
        this.data.coverUrl = t.thumbTempFilePath, this.uploadImageM(), void this.setData({
            videoUrl: t.tempFilePath
        })));
    },
    resetVideo: function() {
        this.setData({
            videoUrl: null
        });
    },
    setSrcAndCaptureImage: function(t) {
        this.data.realVideo = t, this.data.realVideo && this.setData({
            videoUrl: null
        }), this.uploadAudioM();
    },
    uploadAudioM: function() {
        var t = this;
        t.setData({
            uploadIng: !0
        });
        var e = wx.getStorageSync("wx_token"), o = wx.getStorageSync("session"), i = a.getNetAddresss("plugin.video-share.frontend.upload.uploadAudio");
        i += "&3rd_session=" + o, wx.uploadFile({
            url: i,
            filePath: this.data.realVideo,
            name: "file",
            formData: {},
            header: {
                "Content-Type": "application/json",
                Accept: "application/json",
                Cookie: "PHPSESSID=" + e
            },
            success: function(e) {
                var a = JSON.parse(e.data);
                if (1 == a.result) {
                    t.data.videoPath = a.data.audio, t.data.file_name = a.data.file_name, t.setData({
                        uploadIng: !1,
                        videoPath: a.data.audio,
                        file_name: a.data.file_name
                    });
                    var o = {
                        title: t.data.title ? t.data.title : "",
                        videoUrl: t.data.videoPath ? t.data.videoPath : "",
                        videoCover: t.data.videoCover ? t.data.videoCover : "",
                        file_name: t.data.file_name ? t.data.file_name : ""
                    };
                    try {
                        wx.setStorageSync("shareVideoPath", JSON.stringify(o));
                    } catch (t) {
                        console.log(t);
                    }
                } else t.setData({
                    videoUrl: null,
                    isVideo: !0,
                    duration: 0,
                    videoCover: "",
                    limit: "",
                    uploadIng: !1,
                    showUploadBtn: !1
                }), wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 2500
                });
            }
        });
    },
    uploadImageM: function() {
        var t = this;
        t.setData({
            imguploadIng: !0
        });
        var e = a.getNetAddresss("upload.uploadPic");
        wx.uploadFile({
            url: e,
            filePath: this.data.coverUrl,
            name: "file",
            formData: {},
            success: function(e) {
                var a = JSON.parse(e.data);
                if (1 == a.result) {
                    t.setData({
                        videoCover: a.data.img_url
                    });
                    var o = {
                        title: t.data.title ? t.data.title : "",
                        videoUrl: t.data.videoPath ? t.data.videoPath : "",
                        videoCover: t.data.videoCover ? t.data.videoCover : ""
                    };
                    try {
                        wx.setStorageSync("shareVideoPath", JSON.stringify(o));
                    } catch (t) {
                        console.log(t);
                    }
                    t.setData({
                        imguploadIng: !1
                    });
                } else t.setData({
                    imguploadIng: !1
                }), wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 1500
                });
            }
        });
    },
    BtnMethos: function() {
        if (this.data.isNeedGoods && a._isTextEmpty(this.data.chooseGoods)) {
            var t = {
                title: this.data.title ? this.data.title : "",
                videoUrl: this.data.videoPath ? this.data.videoPath : "",
                videoCover: this.data.videoCover ? this.data.videoCover : "",
                file_name: this.data.file_name ? this.data.file_name : "",
                category_id: this.data.category_id ? this.data.category_id : "",
                category_name: this.data.category_name ? this.data.category_name : ""
            };
            try {
                wx.setStorageSync("shareVideoPath", JSON.stringify(t));
            } catch (t) {
                console.log(t);
            }
            this.setData({
                showGoods: !0
            });
        } else {
            if (!this.data.checked) return void a.tips("请勾选用户协议");
            if (a._isTextEmpty(this.data.title)) return void wx.showToast({
                title: "请输入4-18个字标题",
                icon: "none",
                duration: 1500
            });
            if (a._isTextEmpty(this.data.videoUrl)) return void wx.showToast({
                title: "请选择视频进行上传！",
                icon: "none",
                duration: 1500
            });
            if (a._isTextEmpty(this.data.videoCover)) return void wx.showToast({
                title: "请选择一张图片作为视频封面图！",
                icon: "none",
                duration: 1500
            });
            var e = wx.getStorageSync("shareVideoPath");
            e = JSON.parse(e);
            var o = {
                goods_id: this.data.goods_id,
                title: this.data.title,
                video: this.data.videoPath,
                cover: this.data.videoCover,
                file_name: this.data.file_name ? this.data.file_name : e.file_name
            };
            this.data.is_show_cate && (o.category_id = this.data.category_id || 0);
            var i = a.getNetAddresss(1 == a.globalData.store_alone_temp ? "plugin.store-alone-temp.frontend.videoShare.video.sub" : "plugin.video-share.frontend.video.sub");
            a._postNetWork({
                url: i,
                data: o,
                success: function(t) {
                    var e = t.data;
                    if (1 == e.result) {
                        wx.showToast({
                            icon: "none",
                            title: e.msg,
                            duration: 1500
                        });
                        try {
                            wx.removeStorageSync("shareVideoPath");
                        } catch (t) {}
                        wx.navigateTo({
                            url: "/packageC/video_goods/VideoList/VideoList"
                        });
                    } else wx.showToast({
                        icon: "none",
                        title: e.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t.msg);
                }
            });
        }
    },
    titleBlur: function(t) {
        var e = t.detail.value;
        e.length < 4 ? wx.showToast({
            icon: "none",
            title: "请输入不小于4个字",
            duration: 1500
        }) : this.setData({
            title: e
        });
    },
    gotoAdd: function() {
        this.setData({
            showGoods: !0
        });
    },
    onReadimge: function() {
        var t = this;
        t.data.imguploadIng && wx.showToast({
            icon: "none",
            title: "图片正在上传请勿点击",
            duration: 1500
        }), wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(e) {
                var a = e.tempFilePaths;
                t.data.coverUrl = a[0], t.uploadImageM();
            }
        });
    },
    getCategory: function() {
        if (this.data.categoryList.length > 0) this.setData({
            showCategory: !0
        }); else {
            var t = this, e = a.getNetAddresss("plugin.video-share.frontend.video.getCategory");
            a._getNetWork({
                url: e,
                data: {},
                success: function(e) {
                    var a = e.data;
                    1 == a.result ? t.setData({
                        categoryList: a.data,
                        categoryFilterList: a.data,
                        showCategory: !0
                    }) : wx.showToast({
                        title: a.msg,
                        icon: "none",
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    onFilterCategory: function(t) {
        var e = this;
        if ("change" == t.type && this.setData({
            searchWord: t.detail
        }), "" != this.data.searchWord) if (this.data.categoryList.find(function(t) {
            return t.name == e.data.searchWord;
        })) {
            var a = new Array(this.data.categoryList.find(function(t) {
                return t.name == e.data.searchWord;
            }));
            this.setData({
                categoryFilterList: a
            });
        } else this.setData({
            categoryFilterList: this.data.categoryList
        }); else this.setData({
            categoryFilterList: this.data.categoryList
        });
    },
    onClickCategory: function(t) {
        console.log(t.currentTarget.dataset), this.setData({
            category_id: t.currentTarget.dataset.id,
            category_name: t.currentTarget.dataset.name,
            showCategory: !1
        });
    },
    onClose: function() {
        this.setData({
            showCategory: !1,
            showGoods: !1
        });
    },
    agreementbtn: function(t) {
        this.setData({
            checked: t.detail
        });
    },
    removeImg: function(t) {
        var e = t.currentTarget.dataset.tag, a = wx.getStorageSync("shareVideoPath");
        a = JSON.parse(a), "img" == e ? (a.videoCover = null, this.setData({
            videoCover: null,
            images: []
        })) : "video" == e && (a.videoUrl = null, a.videoPath = null, this.setData({
            videoUrl: null,
            videoPath: null
        })), wx.setStorageSync("shareVideoPath", JSON.stringify(a));
    },
    gotoAgreement: function() {
        this.setData({
            showAgreement: !0
        });
    },
    popClose: function() {
        this.setData({
            showAgreement: !1
        });
    },
    getAgreement: function() {
        var t = this, e = a.getNetAddresss("plugin.video-share.frontend.set.getAgreement");
        a._getNetWork({
            url: e,
            data: {},
            success: function(e) {
                var a = e.data;
                1 == a.result ? t.setData({
                    protocol_content: a.data.agreement
                }) : t.setData({
                    protocol_content: a.msg
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    }
});